/**
 *InputProcesser.java
 *@anthor Daniel Liu
 *2006-5-10 ����04:45:43
 **/
package org.lex.swing.fontchooser;

import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Position;

public class InputCompletion implements DocumentListener {
	private JList list = null;
	private JTextField textField = null;

	public InputCompletion(JList list, JTextField field) {
		this.list = list;
		this.textField = field;
	}

	public void changedUpdate(DocumentEvent e) {
		completion(e);
	}

	public void insertUpdate(DocumentEvent e) {
		completion(e);
	}

	public void removeUpdate(DocumentEvent e) {
		completion(e);
	}

	private void completion(DocumentEvent e) {
		String text = textField.getText().trim();
		int index = list.getNextMatch(text, 0, Position.Bias.Forward);
		if (index < 0) {
			index = -1 - index;
			return;
		} else {
			// String name = list.getModel().getElementAt( index ).toString();
			// System.out.println( "Text: " + text + "\tCompletion: " + name );
			// list.ensureIndexIsVisible( index );
		}
	}
}
